View Javadoc

1   /*
2    * Copyright (C) 1998-2000 Semiotek Inc.  All Rights Reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted under the terms of either of the following
6    * Open Source licenses:
7    *
8    * The GNU General Public License, version 2, or any later version, as
9    * published by the Free Software Foundation
10   * (http://www.fsf.org/copyleft/gpl.html);
11   *
12   *  or
13   *
14   * The Semiotek Public License (http://webmacro.org/LICENSE.)
15   *
16   * This software is provided "as is", with NO WARRANTY, not even the
17   * implied warranties of fitness to purpose, or merchantability. You
18   * assume all risks and liabilities associated with its use.
19   *
20   * See www.webmacro.org for more information on the WebMacro project.
21   */
22  
23  package org.webmacro;
24  
25  import org.webmacro.ResourceException;
26  import org.webmacro.Template;
27  import org.webmacro.resource.AbstractTemplateLoader;
28  import org.webmacro.resource.CacheElement;
29  
30  import java.io.File;
31  import java.net.URL;
32  import java.net.MalformedURLException;
33  
34  /***
35   * Implementation of TemplateLoader that loads templates from a given URL.
36   * Objects of this class are responsible for searching exactly one
37   * directory for templates. If it handles a request, it takes URL as
38   * the base path to find the template.
39   * @author Marc Palmer (marc@anyware.co.uk)
40   */
41  public class URLTemplateLoader extends AbstractTemplateLoader
42  {
43  
44      private URL baseURI;
45  
46      public void setConfig (String config)
47      {
48          try
49          {
50              this.baseURI = new URL(config);
51          }
52          catch (MalformedURLException e)
53          {
54              log.error("Cannot init url template loader, bad URL", e);
55          }
56      }
57  
58      /***
59       * Tries to load a template by interpreting query as
60       * a path relative to the path set by setPath.
61       */
62      public final Template load (String query, CacheElement ce) throws ResourceException
63      {
64          try
65          {
66              URL url = new URL(baseURI, query);
67              if (log.loggingDebug())
68              {
69                  log.debug("FileTemplateProvider: Found template " + url);
70              }
71              return helper.load(url, ce);
72          }
73          catch (MalformedURLException e)
74          {
75              throw new ResourceException("Bad template URL", e);
76          }
77      }
78  }